<template>
  <div>
    <el-card>
      <div style="margin-bottom: 20px">
        <el-button type="primary" @click="openDialog()">新增商品</el-button>
        <el-button type="danger" @click="deleteBatch" :disabled="!multipleSelection.length">批量删除</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="productNo" label="商品编号" width="120" />
        <el-table-column prop="productName" label="商品名称" />
        <el-table-column prop="price" label="价格" />
        <el-table-column prop="quantity" label="库存" />
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="openDialog(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteProduct(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px; text-align: right;">
        <el-pagination
          background
          layout="prev, pager, next, total"
          :total="total"
          :page-size="pageSize"
          :current-page.sync="currentPage"
          @current-change="fetchData"
        />
      </div>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form" ref="form" :rules="rules" label-width="100px">
        <el-form-item label="商品编号" prop="productNo">
          <el-input v-model="form.productNo" />
        </el-form-item>
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input type="number" v-model.number="form.price" />
        </el-form-item>
        <el-form-item label="库存" prop="quantity">
          <el-input type="number" v-model.number="form.quantity" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addProduct, updateProduct, getProduct } from '@/api/system/product'

export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      dialogVisible: false,
      dialogTitle: '',
      form: {
        id: null,
        productNo: '',
        productName: '',
        price: null,
        quantity: null
      },
      rules: {
        productNo: [{ required: true, message: '请输入商品编号', trigger: 'blur' }],
        productName: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
        quantity: [{ required: true, message: '请输入库存', trigger: 'blur' }]
      },
      multipleSelection: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getProduct({ page: this.currentPage, pageSize: this.pageSize }).then(res => {
        this.tableData = res.content
        this.total = res.totalPages
      })
    },
    openDialog(row) {
      if (row) {
        this.dialogTitle = '编辑商品'
        this.form = { ...row }
      } else {
        this.dialogTitle = '新增商品'
        this.form = { id: null, productNo: '', productName: '', price: null, quantity: null }
      }
      this.dialogVisible = true
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        const api = this.form.id ? updateProduct : addProduct
        api(this.form).then(() => {
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.fetchData()
        })
      })
    },
    deleteProduct(id) {
      this.$confirm('确定删除该商品吗？', '提示').then(() => {
        deleteProduct(id).then(() => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    },
    deleteBatch() {
      const ids = this.multipleSelection.map(item => item.id)
      this.$confirm('确定批量删除选中商品吗？', '提示').then(() => {
        deleteBatch(ids).then(() => {
          this.$message.success('批量删除成功')
          this.fetchData()
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>
